#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX_ITEMS_IN_STORE 2000
char buffer[1024]; // A buffer for I/O
int N=0;           // The number of cases
int C=0;           // The Credit for the store
int I=0;           // The number of Items in the store
int g_CurrentCase=0; // The current case index
FILE *input=0;
double M=0;           // Contains N/2

#ifdef DEBUG
  #define debug_printf(A) printf A 
#else
  #define debug_printf(A)  
#endif
/* Data structures definition */
typedef struct _Item {
  int val;   // The value of the item
  int index; // The original index of the item
} Item,*pItem;

int compareItems(void *item1, void *item2) {
  int retVal = 0;
  if( ((Item*)item1)->val < ((Item*)item2)->val ) {
    retVal = -1;
  } else if ( ((Item*)item1)->val == ((Item*)item2)->val ) {
    retVal = 0;
  } else {
  retVal = 1;
  }
  return retVal;
}

int nbItems;
Item Store[MAX_ITEMS_IN_STORE];

int ProcessCase() {
  g_CurrentCase++;
  debug_printf(("Processing case #%03d\n",g_CurrentCase));
  // Line 1/3
  // Read the credit in the store: C
  fscanf(input, "%d\n", &C);
  debug_printf(("Credit: %d\n", C));
  
  // Line 2/3
  // Read the number of items in the store: I
  fscanf(input, "%d\n", &I);
  debug_printf(("Items #: %d\n", I));

  // Line 3/3
  // Read each item of the store
  fgets(buffer, 1024, input);
  buffer[strlen(buffer) - 1] = '\0'; // For debug printing remove ending \n
  debug_printf(("Items: [%s]\n", buffer));
  // Parse the line containing the items
  char *current = buffer;
  char *last = buffer;
  char localBuffer[128];
  int index = 0;
  
  while(index < I) {
    while(*current != ' ' && *current != '\0') {
      current++;
    }
    // Store the item
    current[0] = '\0';
    strcpy(localBuffer, last);
    
    debug_printf(("Read token: [%s]\n",localBuffer));
    // Store the current item
    Store[index].index = index;
    Store[index].val = atoi(localBuffer);
    current++;
    last = current;
    index++;
  }

  return 0;
}

int main(int argc, char *argv[]) {
  
  input = fopen(argv[1],"r");
  
  // Read the number of cases
  fscanf(input, "%d\n", &N);
  debug_printf(("Number of cases: %d\n", N));
  
  // Process each case
  int i;
  for(i=0; i<N; i++) {
    ProcessCase();
  }
  
  return 0;
}
